<template>
    <div>
        <Category :scene="scene"></Category>
        <el-card style="margin: 10px 0px;">
            <div v-show="scene == 0">
                <el-button type="primary" size="default" @click="addSpu()" icon="Plus"
                    :disabled="CategoryStore.c3id ? false : true">添加SPU</el-button>
                <el-table border style="margin: 10px 0px;" :data="records">
                    <el-table-column label="序号" type="index" width="80px" align="center">
                    </el-table-column>
                    <el-table-column label="SPU名称" prop="spuName">
                    </el-table-column>
                    <el-table-column label="SPU描述" prop="description" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template #="{ row, $index }">
                            <el-button type="primary" size="small" icon="Plus" title="添加SKU"
                                @click="addSku(row)"></el-button>
                            <el-button type="primary" size="small" icon="Edit" title="修改SPU"
                                @click="updatSpu(row)"></el-button>
                            <el-button type="primary" size="small" icon="View" title="查看SKU列表"
                                @click="findSku(row)"></el-button>
                            <el-popconfirm :title="`你确定删除${row.spuName}?`" @confirm="deleteSpu(row)" width="200px">
                                <template #reference>
                                    <el-button type="primary" size="small" icon="Delete" title="删除SPU" ></el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页器layout进行排序 ->,sizes,total顶到右边-->
                <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]"
                    :background="true" layout=" prev, pager, next, jumper,->,sizes,total" :total="total"
                    @size-change="changeSize" @current-change="getHasSpu" />
                <!--  @current-change="getHasSpu" 会把页码注入到函数内-->
            </div>
            <!-- 绑定自定义事件 -->
            <spuform ref="spu" v-show="scene == 1" @changeScene="changeScene"></spuform>

            <skuform ref="sku" v-show="scene == 2" @changeScene="changeScene"></skuform>
            <!-- dialog对话框 -->
            <el-dialog v-model="show" title="SKU列表" width="800">
                <el-table :data="skuArr" border>
                    <el-table-column prop="skuName" label="sku" width="150" />
                    <el-table-column prop="price" label="sku价格" width="200" />
                    <el-table-column prop="weight" label="sku重量" />
                    <el-table-column label="sku图片">
                        <template #="{ row, $index }">
                            <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px;">
                        </template>
                    </el-table-column>

                </el-table>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { ref, watch ,onBeforeUnmount} from 'vue';
import useCategoryStore from '@/store/modules/category';
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu';
import type { HasSpuResponseData, Records, SpuData, SkuInfoData, SkuData } from '@/api/product/spu/type';
// 引入子组件
import spuform from './spuform.vue';
import skuform from './skuform.vue';
import { ElMessage } from 'element-plus';

let CategoryStore = useCategoryStore();
// 控制三级下拉菜单是否禁用
let scene = ref<number>(0);
// 分页器默认页码
let pageNo = ref<number>(1);
// 每一页展示几条数据
let pageSize = ref<number>(3);
//存储已有的SPU的数据
let records = ref<Records>([]);
// 存储total
let total = ref<number>(0);
// 获取子组件实例
let spu = ref<any>();
let sku = ref<any>();
//存储全部的SKU数据
let skuArr = ref<SkuData[]>([]);
let show = ref<boolean>(false);
// 监听c3Id，发送获取数据请求
watch(() => CategoryStore.c3id, () => {
    // 保证三级分类存在
    if (!CategoryStore.c3id) return;
    getHasSpu()
})
// 封装获取数据请求
const getHasSpu = async (pager = 1) => {
    pageNo.value = pager;
    let result: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, CategoryStore.c3id);
    if (result.code == 200) {
        records.value = result.data.records;
    }
    total.value = result.data.total;
}
//每一页展示几条数据改变时触发的函数
const changeSize = () => {
    getHasSpu();
}
// 添加Spu按钮触发的回调函数
const addSpu = () => {
    scene.value = 1;
    spu.value.initAddSpu(CategoryStore.c3id);
}
// 修改SPU触发的回调函数
const updatSpu = (row: SpuData) => {
    scene.value = 1;
    spu.value.initHasSpuData(row);


}
// 子组件SpuForm绑定自定义事件：目前是让子组件通知父组件切换场景0
const changeScene = (obj: any) => {
    // 子组件传递来的参数num
    scene.value = obj.flag;
    if (obj.params == 'update') {
        // 更新留在当前页面
        getHasSpu(pageNo.value)
    } else {
        getHasSpu();
    }

}
// 切换到添加Sku的界面
const addSku = (row: SpuData) => {
    scene.value = 2;
    sku.value.initSkuData(CategoryStore.c1id, CategoryStore.c2id, row);
}
// 查看品牌全部SKU
const findSku = async (row: SpuData) => {
    let result: SkuInfoData = await reqSkuList((row.id as number));
    if (result.code == 200) {
        skuArr.value = result.data;
        // 显示对话框
        show.value = true;
    }
}
// 删除SPU
const deleteSpu = async (row: SpuData) => {
    let result:any=await reqRemoveSpu((row.id as number));
    if(result.code==200){
        ElMessage({
            type:'success',
            message:'删除成功'
        })
        // 获取剩余SPU
        getHasSpu(records.value.length>1?pageNo.value:pageNo.value-1)
    }else{
        ElMessage({
            type:'error',
            message:'删除失败'
        })
    }
}
// 路由组件销毁前清空数据
onBeforeUnmount(()=>{
    CategoryStore.$reset();
})
</script>

<style scoped></style>